<?php
namespace App\Models;

/**
 * 系统内容分类对象
 */
class MSlugClassify extends BaseModel
{
    public $table = "slug_classify";

    //表关系区===========================================================================

    /**
     * 父类别
     */
    public function parent()
    {
        return $this->belongsTo(MSlugClassify::class, "pid", "id");
    }

    //数据验证区===========================================================================
    /**
     * 站点信息存储验证
     * $flag 0 = rules  1 = msg
     */
    public static function editRulesWithMsg($flag)
    {
        $rules = array(
            "slug" => "required|max:100",
            "title" => "required|max:100",
            'intro' => 'required|max:255',
            'parent' => 'numeric',
        );

        $msg = array();

        if ($flag == 0) {
            return $rules;
        } else {
            return $msg;
        }
    }

    //数据逻辑区===========================================================================

    /**
     * 保存或更新
     */
    public function saveData($parent, $slug, $title, $intro)
    {
        if (!empty($parent)) {
            $this->pid = $parent->id;
            $this->ptitle = $parent->title;
            $this->level = $parent->level + 1;
        } else {
            $this->level = 1;
        }

        $this->slug = $slug;
        $this->title = $title;
        $this->intro = $intro;
        $this->time = time();

        return $this->save();
    }

}